# cjps reviewer invitations analysis
library(tidyverse)
library(rio)
library(broom)
library(stargazer)
library(ggpubr)

#figure 1
obs <- rio::import("obs.dta")
obs$outsider <- ifelse(obs$canada==0, ifelse(obs$familiar==0,1,0),0)
m.fam <- obs %>% group_by(familiar) %>% summarise(mean = mean(dv)) %>%
  mutate(cat = c("No", "Yes"), id = "Familiarity")
m.out <- obs %>% group_by(outsider) %>% summarise(mean = mean(dv)) %>%
  mutate(cat = c("No", "Yes"), id = "CJPS Outsider")
m.can <- obs %>% group_by(canada) %>% summarise(mean = mean(dv)) %>%
  mutate(cat = c("No", "Yes"), id = "Canadian")
m.rank <- obs %>% group_by(rank) %>% summarise(mean = mean(dv)) %>%
  filter(rank<=3) %>%
  mutate(cat = c("Junior", "Middle", "Senior"), id = "Career Stage")
bind_rows(m.fam, m.can, m.rank, m.out) %>% dplyr::select(cat, id, mean) %>%
  ggplot(., aes(x=cat, y=mean)) + 
  geom_bar(stat="identity") + 
  facet_wrap(.~id, scales="free_x", nrow=1) + 
  geom_text(aes(label=paste((round(mean,digits=2)*100),"%",sep=""), vjust=2), color="white") + 
  theme_minimal() + xlab("") + ylab("") + 
  theme(axis.text.y=element_blank(), strip.text.x = element_text(hjust = 0), panel.border = element_rect(colour = "black", fill=NA, size=.5)) + 
  coord_cartesian(ylim=c(0,.65))
ggsave("figure1.tiff", width=8, height=3)

#figure 2
exp <- rio::import("exp.dta")
exp$outsider <- ifelse(exp$canada==0, ifelse(exp$familiar==0,1,0),0)
em.all <- exp %>% group_by(group) %>% summarise(mean = mean(dv)) 
em.out <- exp %>% filter(outsider==1) %>% group_by(group) %>% summarise(mean = mean(dv))
p1 <- ggplot(em.all, aes(x=group, y=mean)) + 
  geom_bar(stat="identity") + 
  geom_text(aes(label=paste((round(mean,digits=2)*100),"%",sep=""), vjust=2), color="white") + 
  theme_bw() + xlab("") + ylab("") + theme(axis.text.y=element_blank()) + 
  coord_cartesian(ylim=c(0,.65)) + ggtitle("Full Sample")
p2 <- ggplot(em.out, aes(x=group, y=mean)) + 
  geom_bar(stat="identity") + 
  geom_text(aes(label=paste((round(mean,digits=2)*100),"%",sep=""), vjust=2), color="white") + 
  theme_bw() + xlab("") + ylab("") + theme(axis.text.y=element_blank()) + 
  coord_cartesian(ylim=c(0,.65)) + ggtitle("Outsiders")
ggarrange(p1,p2)
ggsave("figure2.tiff", width=9, height=4)

#figure 3
fit1 <- lm(dv ~ group*familiar, data=exp)
fit2 <- lm(dv ~ group*canada, data=exp)
fit3 <- lm(dv ~ group*outsider, data=exp)
mat1 <- expand.grid(group = c("Control", "Personal Connection", "Prestige", "Recognition"),familiar = c(0,1))
mat2 <- expand.grid(group = c("Control", "Personal Connection", "Prestige", "Recognition"),canada = c(0,1))
mat3 <- expand.grid(group = c("Control", "Personal Connection", "Prestige", "Recognition"),outsider = c(0,1))
r1 <- cbind(mat1, predict(fit1, newdat=mat1, interval="confidence")) %>%
  mutate(iv = rep(c("Not familiar", "Familiar"), each=4)) 
r2 <- cbind(mat2, predict(fit2, newdat=mat2, interval="confidence")) %>%
  mutate(iv = rep(c("Not in Canada", "In Canada"), each=4))
r3 <- cbind(mat3, predict(fit3, newdat=mat3, interval="confidence")) %>%
  mutate(iv = rep(c("Insider", "Outsider"), each=4))
p1 <- ggplot(r1, aes(x=group, y=fit, ymin=lwr, ymax=upr, color=iv)) + 
  geom_pointrange(position=position_dodge(width=0.5)) + 
  geom_text(aes(label=round(fit, digits=2)), position=position_dodge(width=1)) + 
  coord_flip() + theme_minimal() + xlab("") + ylab("") + 
  theme(legend.position = "bottom", legend.title = element_blank(), panel.border = element_rect(colour = "black", fill=NA, size=.5)) + 
  ggtitle("Reviewer Familiarity")
p2 <- ggplot(r2, aes(x=group, y=fit, ymin=lwr, ymax=upr, color=iv)) + 
  geom_pointrange(position=position_dodge(width=0.5)) + coord_flip() + theme_minimal() + xlab("") + ylab("") + 
  geom_text(aes(label=round(fit, digits=2)), position=position_dodge(width=1)) + 
  theme(legend.position = "bottom", legend.title = element_blank(), axis.text.y = element_blank(), panel.border = element_rect(colour = "black", fill=NA, size=.5)) + 
  ggtitle("Reviewer Location")
p3 <- ggplot(r3, aes(x=group, y=fit, ymin=lwr, ymax=upr, color=iv)) + 
  geom_pointrange(position=position_dodge(width=0.5)) + coord_flip() + theme_minimal() + xlab("") + ylab("") + 
  geom_text(aes(label=round(fit, digits=2)), position=position_dodge(width=1)) + 
  theme(legend.position = "bottom", legend.title = element_blank(), axis.text.y = element_blank(), panel.border = element_rect(colour = "black", fill=NA, size=.5)) + 
  ggtitle("Reviewer Relationship")
ggarrange(p1, p2, p3, nrow=1, widths=c(1.5,1,1))
ggsave("figure3.tiff", width=8.5, height=6)